extends Control


var top_json = {
		"resource":{
			# key：{}
		},
		"map":{
			
		},
		"files": []
	}
	

const map_path = "res://map/main/game/"

const TscnColumn = preload("res://tool/page/map/TscnColumn.tscn")

onready var search_input = $searchBox/LineEdit

func _ready():
	package_map(map_path, top_json)
	
	state_update()
	pass 



func state_update():
	update_list(DataSource.get_data_list(DataSource.map_register))
	


func update_list(map_list):
	var box = $table/ScrollContainer/VBoxContainer
	
	for child in box.get_children():
		child.queue_free()
		
	for data in map_list:
		var ui_item = TscnColumn.instance()
		ui_item.set_data(data)
		box.add_child(ui_item)
		
		ui_item.connect("view", self, "_on_view")
		ui_item.connect("edit", self, "_on_edit")
		ui_item.connect("delete", self, "_on_delete")
	
	pass
	


# 打包地图
func package_map(pre_dir, top_json):
	# 打开目录并读取所有文件  
	var dir = Directory.new()  
	if dir.open(pre_dir) == 0:
		dir.list_dir_begin(true)
		var file_name = dir.get_next()
		
		while file_name != "":
			if dir.current_is_dir():
				package_map(str(pre_dir,file_name,"/"), top_json)
			else:
				if file_name.ends_with(".tscn"):
					top_json["files"].append({
						"file_path": str(pre_dir,file_name),
						"name": file_name,
					})
	
			file_name = dir.get_next()
	else:
		print("尝试访问路径时出错。")


func _on_addBtn_button_up():
	var ui = MainTool.getUILayer().add_ui("res://tool/common/FileSelect/FileSelectModal.tscn")
	
	ui.set_current_dir(map_path)
	ui.connect("file_selected", self, "_on_file_selected")


func _on_file_selected(path):
	MainTool.getUILayer().pop_ui()
	
	var arr = path.split("/")
	var file_name = arr[arr.size()-1].replace(".tscn", "")
	
	var one = DataSource.get_data_by_one(DataSource.map_register, path, "path")
	
	if one:
		MainTool.toast_info("该地图已经存在，无法重新注册")
	else:
		
		var ui = MainTool.getUILayer().add_ui("res://tool/page/map/modal/RegisterMapModal.tscn", 0, false)
		
		ui.set_edit_data(null)
		ui.set_tscn_path(path)
		
		ui.connect("close", self, "state_update")
		


func _on_view(map_register):
	var ui = MainTool.getUILayer().add_ui("res://tool/page/map/mapView/MapView.tscn", 0, false)
	
	ui.set_data(map_register)

func _on_edit(map_data):
	var ui = MainTool.getUILayer().add_ui("res://tool/page/map/modal/RegisterMapModal.tscn", 0, false)
	
	ui.set_edit_data(map_data)
	ui.set_tscn_path(map_data["path"])
	
	ui.connect("close", self, "state_update")

func _on_delete(data):
	MainTool.confirm("删除操作将，删除一系列地图相关关联数据，重新导入后无法恢复，是否确认删除地图: %s \n "%data["name"], self, "_on_del_ok", null, data)

func _on_del_ok(data):

	DataSource.delete_data_by(DataSource.map_area, data["id"], "mapId")
	DataSource.delete_data_by(DataSource.map_sprite_area, data["id"], "mapId")
	DataSource.delete_data_by(DataSource.map_sprite_area_item, data["id"], "mapId")
	DataSource.delete_data_by(DataSource.map_trees, data["id"], "mapId")
	DataSource.delete_data_by(DataSource.map_door, data["id"], "mapId")
	DataSource.delete_data_by(DataSource.map_prop, data["id"], "mapId")
	DataSource.delete_data_by(DataSource.map_interactive, data["id"], "mapId")
	DataSource.delete_data_by(DataSource.map_center_recover, data["id"], "mapId")
	DataSource.delete_data_by(DataSource.map_npc, data["id"], "mapId")
	DataSource.delete_data_by(DataSource.map_decoration, data["id"], "mapId")
	
	# 删除一系列其它数据
	DataSource.delete_data_by(DataSource.map_register, data["id"])
	
	DataSource.delete_data_by(DataSource.map_register, data["id"])
	state_update()
	
	MainTool.toast_info("删除成功!")


# 点击搜索
func _on_search_button_up():
	var text = search_input.text
	
	if text:
		var list = []
		
		for map in DataSource.get_data_list(DataSource.map_register):
			if map["name"].find(text) != -1:
				list.append(map)
				
		update_list(list)
	else:
		update_list(DataSource.get_data_list(DataSource.map_register))
	
		
